package banding.com.google.Zxing.scanview;

import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Vibrator;
import android.util.Log;

import com.google.zxing.R;

import java.io.IOException;

import static android.content.Context.VIBRATOR_SERVICE;

/**
 * @author Yue
 * @date 2017/8/3 17:14
 */
public class BeepSoundMgr {
    private static final float BEEP_VOLUME = 0.10f;
    private static final long VIBRATE_DURATION = 200L;

    private boolean playBeep;
    private boolean vibrate;

    private CaptureActivity captureActivity;
    private MediaPlayer mediaPlayer;

    //getter 和sertter
    public boolean isPlayBeep() {
        return playBeep;
    }
    public void setPlayBeep(boolean playBeep) {
        this.playBeep = playBeep;
    }
    public boolean isVibrate() {
        return vibrate;
    }
    public void setVibrate(boolean vibrate) {
        this.vibrate = vibrate;
    }
    private BeepSoundMgr(CaptureActivity captureActivity) {
        this.captureActivity = captureActivity;
    }
    public static BeepSoundMgr newInstance(CaptureActivity captureActivity) {
        return new BeepSoundMgr(captureActivity);
    }

    void initBeepSound() {
        if (playBeep && mediaPlayer == null) {
            // The volume on STREAM_SYSTEM is not adjustable, and users found it
            // too loud,
            // so we now play on the music stream.
            captureActivity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnCompletionListener(beepListener);

            AssetFileDescriptor file = captureActivity.getResources().openRawResourceFd(R.raw.beep);
            try {
                mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
                file.close();
                mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
                mediaPlayer.prepare();
            } catch (IOException e) {
                mediaPlayer = null;
            }
        }
    }
    /**
     * When the beep has finished playing, rewind to queue up another one.
     */
    private final OnCompletionListener beepListener = new OnCompletionListener() {
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.seekTo(0);
        }
    };

    void playBeepSoundAndVibrate() {
        if (playBeep && mediaPlayer != null) {
            mediaPlayer.start();
            Log.d(">>>","播放音乐");

        }
        if (vibrate) {
            Vibrator vibrator = (Vibrator) captureActivity.getSystemService(VIBRATOR_SERVICE);
            vibrator.vibrate(VIBRATE_DURATION);
        }
    }
    void release(){
        if(mediaPlayer!= null){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null ;
        }
        captureActivity = null;
    }

}
